package com.hkd.user_service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkd.common.api.ApiResponse;
import com.hkd.user_service.api.SysRoleMenuService;
import com.hkd.user_service.api.SysRoleService;
import com.hkd.user_service.api.entity.SysMenu;
import com.hkd.user_service.api.entity.SysRole;
import com.hkd.user_service.api.entity.SysRoleMenu;
import com.hkd.user_service.api.entity.SysUserRole;
import com.hkd.user_service.mapper.SysMenuMapper;
import com.hkd.user_service.mapper.SysRoleMapper;
import com.hkd.user_service.mapper.SysRoleMenuMapper;
import com.hkd.user_service.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author HKD
 * @date 2022/8/20 19:08
 */
@RestController
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysMenuMapper menuMapper;
    @Autowired
    private SysRoleMenuMapper roleMenuMapper;
    @Override
    public ApiResponse insertRole(SysRole role) {
        baseMapper.insert(role);
        return ApiResponse.ok();
    }

    @Override
    public ApiResponse<List<String>> getRoleByUserId(String id) {
        LambdaQueryWrapper<SysUserRole> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(SysUserRole::getUserId,id);
        List<SysUserRole> userRoles = userRoleMapper.selectList(userRoleWrapper);
        List<String> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        List<String> roleNames = this.listByIds(roleIds).stream().map(SysRole::getRoleName).collect(Collectors.toList());
        return ApiResponse.ok(roleNames);
    }

    @Override
    public ApiResponse<List<String>> getAuthPathByUserId(String userId) {
        LambdaQueryWrapper<SysUserRole> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.eq(SysUserRole::getUserId,userId);
        List<SysUserRole> userRoles = userRoleMapper.selectList(userRoleWrapper);
        List<String> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        LambdaQueryWrapper<SysRoleMenu> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.in(SysRoleMenu::getRoleId,roleIds);
        List<SysRoleMenu> roleMenus = roleMenuMapper.selectList(roleMenuWrapper);
        List<String> menuIds = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        List<SysMenu> menus = menuMapper.selectBatchIds(menuIds);
        List<String> authPath = menus.stream().map(SysMenu::getLinkUrl).collect(Collectors.toList());
        authPath=authPath.stream().filter(item->item!=null).collect(Collectors.toList());
        return ApiResponse.ok(authPath);
    }
}
